package com.zhitong.money.mapper;

import com.zhitong.money.entity.Salary;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface SalaryMapper {

    @Results(id = "salaryMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "date",property = "date"),
            @Result(column = "tid",property = "tid"),
            @Result(column = "basic_salary",property = "basicSalary"),
            @Result(column = "full_attention",property = "fullAttention"),
            @Result(column = "subsidy",property = "subsidy"),
            @Result(column = "position_salary",property = "positionSalary"),
            @Result(column = "overTime",property = "overTime"),
            @Result(column = "overTime_Salary",property = "overTimeSalary"),
            @Result(column = "total_pay",property = "totalPay"),
            @Result(column = "leave_time",property = "leaveTime"),
            @Result(column = "leave_deduction",property = "leaveDeduction"),
            @Result(column = "late_time",property = "lateTime"),
            @Result(column = "late_deduction",property = "lateDeduction"),
            @Result(column = "early_time",property = "earlyTime"),
            @Result(column = "early_deduction",property = "earlyDeduction"),
            @Result(column = "absence_day" ,property = "absenceDay"),
            @Result(column = "other_deduction",property = "otherDeduction"),
            @Result(column = "total_deduction",property = "totalDeduction"),
            @Result(column = "tax_rate",property = "taxRate"),
            @Result(column = "tax_salary",property = "taxSalary"),
            @Result(column = "total",property = "total"),
            @Result(column = "result",property = "result"),
    })
    @Select("select salary.*,employee.name,employee.pid from salary,employee where tellerId = tid")
    public List<Salary> findAllSalary();

    @ResultMap("salaryMap")
    @Select({
            "<script>",
            "select salary.*,employee.name,employee.pid from salary,employee",
            "<where>",
            "tellerId = tid",
            "<if test='tid!=null and tid!=\"\"'>and tid=#{tid}</if>",
            "<if test='name!=null and name!=\"\"'>and name like concat(concat('%',#{tid},'%'))</if>",
            "<if test='pid!=null and pid!=\"\"'>and pid=#{pid}</if>",
            "<if test='result!=null and result !=\"\"'>and result=#{result}</if>",
            "<if test='month!=null and month !=\"\"'>and Month(date)=#{month}</if>",
            "<if test='year!=null and year!=\"\"'>and YEAR(date)=#{year}</if>",
            "</where>",
            "</script>"
    })
    public List<Salary> findSalaryByMap(Map<String,Object> map);

    @ResultMap("salaryMap")
    @Select("select salary.*,employee.name,employee.pid from salary,employee where salary.id =#{id} and tellerId = tid")
    public Salary findSalaryById(String id);

    @ResultMap("salaryMap")
    @Select("select salary.*,employee.name,employee.pid from salary,employee where tid=#{tid} and tellerId = tid")
    public List<Salary> findSalaryByTid(String tid);

    @ResultMap("salaryMap")
    @Select("select * from salary where tid=#{tid} and date=#{time}")
    public Salary findSalaryByTidAndTime(String tid, String time);

    @Insert("insert into salary values(#{id},#{date}," +
            "#{tid},#{basicSalary},#{fullAttention},#{subsidy}," +
            "#{positionSalary},#{overTime},#{overTimeSalary},#{totalPay}," +
            "#{leaveTime},#{leaveDeduction},#{lateTime},#{lateDeduction},#{earlyTime},#{earlyDeduction},#{absenceDay},#{otherDeduction}," +
            "#{totalDeduction},#{taxRate},#{taxSalary},#{total},#{result})")
    public void addSalary(Salary salary);


    @Update({
            "<script>",
            "update salary",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='date!=null'> date=#{date},</if>",
            "<if test='basicSalary!=null'> basic_salary=#{basicSalary},</if>",
            "<if test='fullAttention!=null'> full_attention=#{fullAttention},</if>",
            "<if test='subsidy!=null'> subsidy=#{subsidy},</if>",
            "<if test='positionSalary!=null'> position_salary=#{positionSalary},</if>",
            "<if test='overTimeSalary!=null'> overTime_salary=#{overTimeSalary},</if>",
            "<if test='totalPay!=null'> total_pay=#{totalPay},</if>",
            "<if test='overTime!=null'> overTime=#{overTime},</if>",
            "<if test='leaveTime!=null'> leave_time=#{leaveTime},</if>",
            "<if test='leaveDeduction!=null'> leave_deduction=#{leaveDeduction},</if>",
            "<if test='lateTime!=null'> late_time=#{lateTime},</if>",
            "<if test='lateDeduction!=null'> late_deduction=#{lateDeduction},</if>",
            "<if test='earlyTime!=null'> early_time=#{earlyTime},</if>",
            "<if test='earlyDeduction!=null'> early_deduction=#{earlyDeduction},</if>",
            "<if test='absenceDay!=null'> absence_day=#{absenceDay},</if>",
            "<if test='otherDeduction!=null'> other_deduction=#{otherDeduction},</if>",
            "<if test='totalDeduction!=null'> total_deduction=#{totalDeduction},</if>",
            "<if test='total!=null'> total=#{total},</if>",
            "<if test='taxRate!=null'> tax_rate=#{taxRate},</if>",
            "<if test='taxSalary!=null'> tax_salary=#{taxSalary},</if>",
            "<if test='result!=null and result!=\"\"'> result=#{result},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updateSalary(Salary salary);


    @Delete("delete from salary where id = #{id}")
    public void delSalary(String id);

    @Delete({
            "<script>",
            "delete from salary ",
            "where id in",
            "<foreach item='item' collection='ids' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delSalaryByIds(List<String> ids);

}
